package com.hobbo.ibus;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;

import com.hobbo.ijkbus.struct.BaseActivity;
import com.hobbo.ijkbus.struct.BaseFragment;
import com.hobbo.ijkbus.struct.FunctionManager;
import com.hobbo.ijkbus.struct.function.FunctionNoParamNoResult;
import com.hobbo.ijkbus.struct.function.FunctionWithParamAndResult;
import com.hobbo.ijkbus.struct.function.FunctionWithResultOnly;


public class MainActivity extends BaseActivity {
    private static String TAG = MainActivity.class.getSimpleName();
    private FragmentManager mSupportFragmentManager;
    private String mCurrFragmentTag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mSupportFragmentManager = getSupportFragmentManager();
        this.findViewById(R.id.home).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Fragment fragment = mSupportFragmentManager.findFragmentByTag(HomeFragment.TAG);
                FragmentTransaction fragmentTransaction = mSupportFragmentManager.beginTransaction();
                fragmentTransaction.hide(mSupportFragmentManager.findFragmentByTag(mCurrFragmentTag));
                if (fragment != null) {
                    if (fragment.isHidden()) {
                        fragmentTransaction.show(fragment).commit();
                    }
                } else {
                    fragmentTransaction.add(R.id.fragment, new HomeFragment(), HomeFragment.TAG).commit();
                }
                mCurrFragmentTag = HomeFragment.TAG;
            }
        });

        this.findViewById(R.id.bt_1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Fragment fragment = mSupportFragmentManager.findFragmentByTag(AFragment.TAG);
                FragmentTransaction fragmentTransaction = mSupportFragmentManager.beginTransaction();
                fragmentTransaction.hide(mSupportFragmentManager.findFragmentByTag(mCurrFragmentTag));
                if (fragment != null) {
                    if (fragment.isHidden()) {
                        fragmentTransaction.show(fragment).commit();
                    }
                } else {
                    fragmentTransaction.add(R.id.fragment, new AFragment(), AFragment.TAG).commit();
                }
                mCurrFragmentTag = AFragment.TAG;
            }
        });

        this.findViewById(R.id.bt_2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Fragment fragment = mSupportFragmentManager.findFragmentByTag(BFragment.TAG);
                FragmentTransaction fragmentTransaction = mSupportFragmentManager.beginTransaction();
                fragmentTransaction.hide(mSupportFragmentManager.findFragmentByTag(mCurrFragmentTag));
                if (fragment != null && fragment.isHidden()) {
                    if (fragment.isHidden()) {
                        fragmentTransaction.show(fragment).commit();
                    }
                } else {
                    fragmentTransaction.add(R.id.fragment, new BFragment(), BFragment.TAG).commit();
                }
                mCurrFragmentTag = BFragment.TAG;
            }
        });
        mSupportFragmentManager.beginTransaction().add(R.id.fragment, new HomeFragment(), HomeFragment.TAG).commit();
        mCurrFragmentTag = HomeFragment.TAG;
    }


    @Override
    public void setFunctionForFragment(String tag) {
        BaseFragment fragment = (BaseFragment) mSupportFragmentManager.findFragmentByTag(tag);
        fragment.setFunctionManager(FunctionManager
                .getInstance()
                .addFunction(new FunctionNoParamNoResult(AFragment.TAG) {
                    @Override
                    public void function() {
                        Log.i(TAG, AFragment.TAG);
                    }
                }).addFunction(new FunctionWithResultOnly<String>(BFragment.TAG) {
                    @Override
                    public String function() {
                        return "i love you";
                    }
                }).addFunction(new FunctionWithParamAndResult<String, Integer>(HomeFragment.TAG) {
                    @Override
                    public Integer function(String message) {
                        Log.i(TAG, "message:" + message);
                        return 0;
                    }
                })
        );
    }
}
